package com.cipe.cmrs.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.bean.PaymentRequestBean;
import com.cipe.cmrs.bean.SignatureReportBean;
import com.cipe.cmrs.model.Cipeproject;
import com.cipe.cmrs.model.Partner;
import com.cipe.cmrs.model.Paymentrequest;
import com.cipe.cmrs.model.SignatureSelectedLog;

import com.cipe.cmrs.util.UserUtil;
import com.cipe.cmrs.model.User;



@Service
@Transactional
public class SignatureReportService extends BaseService<SignatureSelectedLog>{
	@Autowired
	PaymentrequestService paymentRequestService;
	
	@Autowired
	CipeprojectService cipeProjectService;
	
	List<SignatureReportBean> signatureReportBeansList;
	SignatureReportBean signatureReportBean;
	List<Paymentrequest> paymentRequestList;
	SignatureSelectedLog signatureSelectedLog;
	
	/**
	 * return list of SignatureReportBean 
	 * @param selectionType
	 * @return
	 */
	public List< SignatureReportBean > neverSelectedSignatures(String selectionType)
	{
		 signatureReportBeansList=new ArrayList<SignatureReportBean>();

	if(selectionType.equalsIgnoreCase("NeverSelected"))	 
	    paymentRequestList = paymentRequestService.getPaymentListForNeverSelectedSignature(); 
	else
		paymentRequestList= paymentRequestService.getPaymentListForUnPaidSignature();
	   if(paymentRequestList.size()>0){	   
	        for(int i=0;i<paymentRequestList.size();i++)
	        {
	        	signatureReportBean=new SignatureReportBean();
	        	signatureReportBean.setPaymentRequest(paymentRequestList.get(i));
	        	signatureReportBean.setPayeeName(paymentRequestList.get(i).getPayee().getPayeeName());
	            Cipeproject cipeProject=cipeProjectService.getCipeProjectByPaymentReqId(paymentRequestList.get(i).getId());
	            if(cipeProject!=null)
	            {
	            	signatureReportBean.setProjectTitle(cipeProject.getProjectTitle());
	            	signatureReportBean.setProjectAcronym(cipeProject.getAcronym());
	            	
	            }
	            else{
	            	signatureReportBean.setProjectTitle("");
	            	signatureReportBean.setProjectAcronym("");
	            	
	            }
	            
	        	signatureReportBeansList.add(signatureReportBean);
	        }
	   }
	   else{	   
		   signatureReportBeansList.add(new SignatureReportBean());
	   }
	   
		return signatureReportBeansList;
	}
	
	public List< SignatureReportBean> unPaidSignatures()
	{
		return signatureReportBeansList;
	}
	/**
	 * 
	 * @param signatureSelectedLog
	 * save Selected Signature to the database
	 */
	public  void saveSignatureSelectedLog(SignatureSelectedLog signatureSelectedLog)
	{
		User user = UserUtil.INSTANCE.getCurrentUser();
		signatureSelectedLog.setCreatedBy(user);
		persist(signatureSelectedLog);
	}
	

}
